﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using ToolKit.Mvvm.Extensions;
using ToolKit.Mvvm.Interfaces;

namespace ToolKit.Mvvm
{
    public abstract class ApplicationBase : Application
    {
        public Container Container { get; set; } = Container.Default;
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);

            Initialize();
        }

        /// <summary>
        /// 内部初始化
        /// </summary>
        /// <exception cref="NotImplementedException"></exception>
        private void Initialize()
        {
            //Container.MainAssembly = SetMainAssembly();//配置主程序集
            Container.ModuleManager.ModuleCatalog = SetModuleCatalog();//配置模块目录
            ConfigureModules(Container.ModuleManager);//手动增加模块
            InitializeModules(Container);//初始化模块
            RegisterTypes(Container);//手动和自动注册主程序集的类型
            Window window = CreateShell();//创建主窗体
            MainWindow = window ?? null;
            MainWindow?.Show();
        }

        protected virtual void InitializeModules(IContainer container)
        {
            container.InitializeModule();
        }

        /// <summary>
        /// 配置模块目录(自动添加模块)
        /// </summary>
        /// <param name="moduleCatalog"></param>
        /// <returns></returns>
        public virtual string SetModuleCatalog(string moduleCatalog = "./Modules")
        {
            return moduleCatalog;
        }

        /// <summary>
        /// 设置主程序集
        /// </summary>
        /// <returns></returns>
        //public abstract Assembly SetMainAssembly();

        /// <summary>
        /// 手动添加模块
        /// </summary>
        /// <param name="moduleManager"></param>
        public virtual void ConfigureModules(IModuleManager moduleManager)
        {

        }

        /// <summary>
        /// 自动注册主程序集中的所有标注DependenceInjection的类型
        /// </summary>
        /// <param name="IContainer"></param>
        public virtual void RegisterTypes(IContainer container)
        {
            //container.Register(Container.MainAssembly.GetDependenceInjectionTypes());
        }

        /// <summary>
        /// 创建主窗体
        /// </summary>
        /// <returns></returns>
        public abstract Window CreateShell();
    }
}
